package com.hz.filemanager.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.airbnb.lottie.LottieAnimationView;
import com.hz.filemanager.R;
import com.hz.filemanager.model.clean.ScanBean;

import java.util.ArrayList;
import java.util.List;

public class ScanAdapter extends RecyclerView.Adapter<ScanAdapter.ViewHolder> {


    private int type ;
    public final static int TYPE_SCAN = 1;
    public final static int TYPE_RESULT = 2;

    private List<ScanBean> datas = new ArrayList<>();
    private OnItemScanCompleted listener;

    public ScanAdapter(int type){
        this.type = type;
    }

    public void setListener(OnItemScanCompleted listener) {
        this.listener = listener;
    }

    public void setDatas(List<ScanBean> datas){
        this.datas = datas;
    }

    public List<ScanBean> getDatas() {
        return datas;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.scan_item,parent,false);
        ScanAdapter.ViewHolder viewHolder = new ScanAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ScanAdapter.ViewHolder holder, int position) {

        bindView(holder, position);

    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {

        if(payloads.isEmpty()){
            onBindViewHolder(holder, position);
        }else{

            bindView(holder, position);
            if(this.listener != null){
                listener.scanComplete(position, datas.get(position).getSize());
            }
        }
    }

    private void bindView(@NonNull ViewHolder holder, int position) {
        final ScanBean scanBean = datas.get(position);
        holder.nameTv.setText(scanBean.getName());

        if(type == TYPE_SCAN){
            holder.av.setVisibility(View.VISIBLE);
            holder.av.setImageAssetsFolder("scan/images");
            holder.av.setAnimation("scan/data.json");
            if (scanBean.isScan()) {
                holder.av.playAnimation();
            } else {
                holder.av.cancelAnimation();
            }
        }else if(type == TYPE_RESULT){
            holder.sizeTv.setVisibility(View.VISIBLE);
            holder.sizeTv.setText(String.valueOf(scanBean.getSize())+"MB");
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{


        private final TextView nameTv;
        private final TextView sizeTv;
        private final LottieAnimationView av;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            nameTv = itemView.findViewById(R.id.name_tv);
            sizeTv = itemView.findViewById(R.id.size_tv);
            av = itemView.findViewById(R.id.lottie_av);

        }
    }

    public interface OnItemScanCompleted{
        void scanComplete(int position, float size);
    }

}
